﻿using System;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using viBlog.Providers;
using visinia.SmartEngine.Providers;
using viBlog.Classes;

namespace viBlog
{
    public class SqlDataProvider : viBlogDataProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;

        public SqlDataProvider()
        {
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        #endregion

        #region POSTS
        public override IDataReader GetPost(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPost", PostId);
        }
        public override IDataReader GetPosts()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPosts");
        }
        public override IDataReader GetPostsAdmin()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostsAdmin");
        }
        public override IDataReader GetPostDescriptionAdmin(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostDescriptionAdmin", PostId);
        }
        public override IDataReader GetPostDescription(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostDescription", PostId);
        }
        public override IDataReader GetPostRelatives(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostRelatives", PostId);
        }

        public override IDataReader AddPost(string Title, string Author, DateTime Date, string Summary, string Url, bool AllowComments, bool IsDraft, string Description)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogAddPost", Title, Author, Date, Summary, Url, AllowComments, IsDraft, Description);
        }

        public override void UpdatePost(int PostId, string Title, string Author, DateTime Date, string Summary, string Url, bool AllowComments, bool IsDraft, string Description)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogUpdatePost", PostId, Title, Author, Date, Summary, Url, AllowComments, IsDraft, Description);
        }

        public override void DeletePost(int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeletePost", PostId);
        }
        
        public override IDataReader GetPostsByCategory(int CategoryId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostsByCategory", CategoryId);
        }
        public override IDataReader GetPostsByTag(int TagId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostsByTag", TagId);
        }
        public override IDataReader GetArchives()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogArchives");
        }
        public override IDataReader GetPostsByDate(int Year, int Month)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetPostsByDate", Year, Month);
        }        
        #endregion

        #region CATEGORIES
        public override IDataReader GetCategories(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetCategoriesByPost", PostId);
        }

        public override IDataReader GetCategories()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetCategories");
        }

        public override IDataReader GetCategoryPostsCount()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetCategoryPostsCount");
        }

        public override void InsertCategory(int CategoryId, int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogInsertCategory", CategoryId, PostId);
        }

        public override void RemoveCategory(int CategoryId, int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogRemoveCategory", CategoryId, PostId);
        }

        public override IDataReader AddCategory(string Category)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogAddCategory", Category);
        }

        public override void UpdateCategory(int CategoryId, string Category)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogUpdateCategory", CategoryId, Category);
        }

        public override void DeleteCategory(int CategoryId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteCategory", CategoryId);
        }
        #endregion

        #region TAGS
        public override IDataReader GetTags()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetTags");
        }
        public override IDataReader GetTags(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetTagsByPostId", PostId);
        }
        public override IDataReader GetTagCount()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetTagCount");
        }
        public override void InsertTag(int TagId, int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogInsertTag", TagId, PostId);
        }
        public override void RemoveTag(int TagId, int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogRemoveTag", TagId, PostId);
        }
        public override IDataReader AddTag(string Name)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogAddTag", Name);
        }

        public override void DeleteTag(int TagId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteTag", TagId);
        }
        #endregion

        #region DATAFILTERS
        public override IDataReader GetFilters()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetFilters");
        }
        public override void AddFilter(int FilterType, int ModuleId, string Data, int ItemsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogAddFilter", FilterType, ModuleId, Data, ItemsCount);
        }
        public override void UpdateFilter(int FilterId, int FilterType, int ModuleId, string Data, int ItemsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogUpdateFilter", FilterId, FilterType, ModuleId, Data, ItemsCount);
        }
        public override void DeleteFilter(int FilterId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteFilter", FilterId);
        }
        #endregion

        #region BLOG SETTINGS
        public override IDataReader GetBlogSettings()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetBlogSettings");
        }
        public override void AddBlogSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogAddBlogSetting", key, value);
        }

        public override void UpdateBlogSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogUpdateBlogSetting", key, value);
        }

        public override void DeleteBlogSetting(string key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteBlogSetting", key);
        }
        #endregion

        #region COMMENTS  
        public override IDataReader GetCommentsAdmin()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetCommentsAdmin");
        }        
        public override IDataReader GetComment(int CommentId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetComment", CommentId);
        }
        public override IDataReader GetComments()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetComments");
        }
        public override IDataReader GetCommentsByPost(int PostId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogGetCommentsByPost", PostId);
        }
        public override IDataReader AddComment(int PostId, string Author, string Email, string Website, DateTime Date, string Content, Comment.CommentStatus Status)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogAddComment", PostId, Author, Email, Website, Date, Content, Status);
        }
        public override IDataReader UpdateComment(int CommentId, int PostId, string Author, string Email, string Website, DateTime Date, string Content, Comment.CommentStatus Status)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viBlogUpdateComment", CommentId, PostId, Author, Email, Website, Date, Content, Status);
        }
        public override void DeleteComment(int CommentId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteComment", CommentId);
        }
        public override void DeleteComments(int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viBlogDeleteComments", PostId);
        }
        #endregion        
    }
}
